import axios from 'axios'
import { getCookie } from './index'
import { message } from 'antd'

const request = axios.create({
    timeout: 3000,
    timeoutErrorMessage: '接口超时'
})

// 请求拦截
request.interceptors.request.use((config) => {

    if(getCookie()) {
        config.headers.authorization = getCookie();
    }

    return config
}, (err) => {
    return Promise.reject(err)
})

// 响应拦截
request.interceptors.response.use((config) => {
    return config.data
}, (err) => {
    const { msg } = err.response.data
    // 错误提示
    message.error(msg)

    return Promise.reject(err)
})

export default request